const db_JQL = uniCloud.databaseForJQL();
const { getQuarter, getQuarterStartEndTimestamp } = require('../../common/time');
const { getRank } = require('../../common/text');
module.exports = async function(options = {}) {
	const { characterId } = options;
	const token = this.getUniIdToken();
	if (!token || !characterId) {
		throw new Error('非法访问');
	}
	const timeResult = getQuarterStartEndTimestamp(getQuarter().year, getQuarter().quarter);
	const [res] = await Promise.all([
		db_JQL.collection('a-dz-complete-stats-list').where({
			characterId,
			createTime: db_JQL.command.gt(timeResult.start).and(db_JQL.command.lt(timeResult.end))
		}).groupBy('characterId').groupField('sum(consumingTime) as learningTime,avg(kpm) as kpm,avg(wpm) as wpm,avg(successWpm) as successWPM,avg(accuracyKey) as accuracyKey,avg(accuracyWord) as accuracyWord').get()
	])
	if (res.data.length) {
		let result = res.data[0];
		result = {
			kpm: Math.round(result.kpm),
			wpm: Math.round(result.wpm),
			successWpm: Math.round(result.successWPM),
			learningTime: Number((result.learningTime / 3600).toFixed(2)),
			accuracy: Number(((result.accuracyKey + result.accuracyWord) / 2).toFixed(2)),
			reportTime: `${getQuarter().year}年Q${getQuarter().quarter}`
		}
		const { rank, appellation } = getRank(result.successWPM, result.accuracyWord);
		result.abilityTitle = appellation.name;
		result.abilityDesc = appellation.desc;
		result.abilityLevel = rank;
		return {
			code: 0,
			data: result,
			msg: '获取成功'
		}
	} else {
		return {
			code: 0,
			data: [],
			msg: '无数据'
		}
	}
}